<?php

class Kd_Html_Grid extends Kd_Html_Element {

	private $_title = '';
    private $_columns = array();
    private $_toolbar = NULL;
    private $_dataSource = NULL;
    private $_primaryKey = '';
    private $_sortField = '';
    private $_sortDirection = 'asc';
    private $_sortUrlFormat = '';
    private $_rowIndex = -1;
    private $_rowCount = 0;
    private $_indexable = TRUE;
    private $_allowCheck = TRUE;
    private $_allowFilter = FALSE;
    private $_customHeadRender = NULL;
    private $_customCellRender = NULL;

    public function __construct() {
        parent::__construct();
        $this->templateName = 'grid';
        $this->addClass('grid');
        $this->_toolbar = new Kd_Html_Toolbar();
    }
    
    public function getTitle(){
    	return $this->_title;
    }
    
    public function setTitle($value){
    	$this->_title = $value;
    	return $this;
    }

    public function getToolbar() {
        return $this->_toolbar;
    }

    public function setHeadRender() {
        $args = func_get_args();
        $function = array_shift($args);
        $this->_customHeadRender = array('function' => $function, 'args' => $args);
        return $this;
    }

    public function getHeadRender() {
        return $this->_customHeadRender;
    }

    public function setCellRender($value) {
        $args = func_get_args();
        $function = array_shift($args);
        $this->_customCellRender = array('function' => $function, 'args' => $args);
        return $this;
    }

    public function getCellRender() {
        return $this->_customCellRender;
    }

    public function getAllowFilter() {
        return $this->_allowFilter;
    }

    public function setAllowFilter($value) {
        $this->_allowFilter = $value;
        return $this;
    }

    public function getDataSource() {
        return $this->_dataSource;
    }

    public function setDataSource($value) {
        $this->_dataSource = $value;
        $this->_rowCount = count($this->_dataSource);
        return $this;
    }

    public function getIndexable() {
        return $this->_indexable;
    }

    public function setIndexable($value) {
        $this->_indexable = $value;
        return $this;
    }

    public function getAllowCheck() {
        return $this->_allowCheck;
    }

    public function setAllowCheck($value) {
        $this->_allowCheck = $value;
        return $this;
    }

    public function getPrimaryKey() {
        return $this->_primaryKey;
    }

    public function setPrimaryKey($value) {
        $this->_primaryKey = $value;
        return $this;
    }

    public function getSortField() {
        return $this->_sortField;
    }

    public function setSortField($value) {
        $this->_sortField = $value;
        return $this;
    }

    public function getSortDirection() {
        return $this->_sortDirection;
    }

    public function setSortDirection($value) {
        $this->_sortDirection = $value;
        return $this;
    }

    public function getSortUrlFormat() {
        return $this->_sortUrlFormat;
    }

    public function setSortUrlFormat($value) {
        $this->_sortUrlFormat = $value;
        return $this;
    }

    public function resetRowIndex() {
        $this->_rowIndex = -1;
        return $this;
    }

    public function isFirstRow() {
        return $this->_rowIndex == 0;
    }

    public function isLastRow() {
        return $this->_rowIndex + 1 == $this->getRowCount();
    }

    public function isEvenRow() {
        return $this->_rowIndex % 2 != 0;
    }

    public function isOddRow() {
        return!$this->isEvenRow();
    }

    public function getRowCount() {
        return $this->_rowCount;
    }

    public function getRowIndex() {
        return $this->_rowIndex;
    }

    public function getCurrentRow() {
        return $this->_dataSource[$this->_rowIndex];
    }

    public function nextRow() {
        $this->_rowIndex++;
        if ($this->_rowIndex >= $this->getRowCount()) {
            return FALSE;
        }
        return $this->getCurrentRow();
    }

    public function resetDataSource() {
        $this->_rowIndex = -1;
        return $this;
    }

    public function addLabelColumn($title, $name, $text) {
        $result = $this->createColumn($title, $name, 'label', 'label');
        $result->getCell()->setText($text);
        return $result;
    }

    public function addLinkColumn($title, $name, $url, $text, $target = '') {
        $result = $this->createColumn($title, $name, 'link', 'label');
        $result->getCell()->setUrl($url)->setText($text)->setTarget($target);
        return $result;
    }

    public function addButtonColumn($title, $name, $script, $text) {
        $result = $this->createColumn($title, $name, 'button', 'label');
        $result->getCell()->setScript($script)->setText($text);
        return $result;
    }

    public function addDatetimeColumn($title, $name, $text, $format = 'Y-m-d') {
        $result = $this->createColumn($title, $name, 'datetime', 'label');
        $result->getCell()->setFormat($format)->setText($text);
        return $result;
    }

    public function addImageColumn($title, $name, $imageUrl, $width = NULL, $height = NULL) {
        $result = $this->createColumn($title, $name, 'image', 'label');
        $result->getCell()->setImageUrl($imageUrl)->setImageWidth($width)->setImageHeight($height);
        return $result;
    }

    public function createColumn($title, $name = '', $cellType = 'label', $headType = 'label') {
        $result = new Kd_Html_Grid_Column();
        $result->setGrid($this)
                ->setTitle($title)
                ->setName($name);
        $result->createHead($headType);
        $result->createCell($cellType);
        $this->_columns[$name] = $result;
        return $result;
    }

    public function removeColumn() {
        $args = func_get_args();
        foreach ($args as $name) {
            if (array_key_exists($name, $this->_columns)) {
                unset($this->_columns[$name]);
            }
        }
        return $this;
    }

    public function getColumns() {
        return $this->_columns;
    }

    public function getColumn($name) {
        if (array_key_exists($name, $this->_columns)) {
            return $this->_columns[$name];
        }
        return NULL;
    }

    protected function render() {
        return '';
    }

}